package main

import (
	"fmt"
	"go-tool/controllers"
	"regexp"
	"strconv"
	"strings"
)

func main() {

	type Page struct {
		path string
		num  int
	}
	arr := []Page{
		{"dianqi", 1},
		{"huanbao", 1},
		{"dianzi", 2},
		{"anfang", 2},
		{"huanbao", 1},
		{"taiyangneng", 3},
	}
	for i := 0; i < len(arr); i++ {
		main1(arr[i].path, arr[i].num)
	}
}

func main1(path string, num int) {
	for i := 1; i <= num; i++ {
		info, err := controllers.Fetch("http://b2b.huangye88.com/pukouqu/" + path + "/pn" + strconv.Itoa(i) + "/")
		fmt.Println(info)
		if err == nil {
			listRe := `<h4><a href="([^"]*?)" title="([^"]*?)"`
			matches := regexp.MustCompile(listRe).FindAllSubmatch([]byte(info), -1)
			for _, v := range matches {
				content := ""
				detail, _ := controllers.Fetch(string(v[1]))
				//fmt.Println(detail)
				s1 := regexp.MustCompile(`https://www.tianyancha.com/company/(\d+)`).FindStringSubmatch(string(detail))
				if s1 != nil {
					content += s1[0]
				}
				//fmt.Println(s1)
				s2 := regexp.MustCompile(`<div class="big"><h1>([^<]*?)</h1>`).FindStringSubmatch(string(detail))
				if s2 != nil {
					content += "," + s2[1]
				}
				s4 := regexp.MustCompile(`<label>联系人：</label><a href="[^"]*" rel="nofollow">([^<]*?)</a>`).FindStringSubmatch(string(detail))
				if s4 != nil {
					content += "," + s4[1]
				}
				//fmt.Println(s2)
				s3 := regexp.MustCompile(`<span class='secret'>([^<]*?)</span>`).FindStringSubmatch(string(detail))
				if s3 != nil {
					phone := ToPhone(s3[1])
					content += "," + phone
				}
				//fmt.Println(s3)
				//fmt.Println(s4)
				controllers.WriteFile("1.csv", content)
			}
		}
	}
	//now := time.Now()
	//startTime := now.AddDate(0, 0, -6).Format("2006-01-02")
	//endTime := now.AddDate(0, 0, -5).Format("2006-01-02")
	////fmt.Println("1111")
	////return
	////components.InitEs("http://10.14.0.4:9200", "http://10.14.0.5:9200", "http://10.14.0.6:9200")
	//components.InitEs("http://localhost:9200/")
	////components.InitEsPrd("http://10.14.0.4:9200" ,"http://10.14.0.5:9200", "http://10.14.0.6:9200")
	////components.InitEsPrd("http://localhost:9200")
	////(&services.JobIrayObj{}).ErgodicFiles()
	////insertEs()
	////(&services.JobInfo{}).JobEachConstructRequest()
	////(&services.JobInfo{}).JobEach()
	////整日耗时
	//(&services.JobLog{}).Each(startTime, endTime)
	////(&services.JobLog{}).EachEs()
	////构建耗时
	//(&services.JobConstructLog{}).Each(startTime, endTime)
	////(&services.JobConstructLog{}).EachEs()
	////ip := "10.20.0.156:7712"
	////ip = ip[: strings.Index(ip, ":")]
	////fmt.Println(ip)
}

func ToPhone(code string) (phone string) {
	str := strings.Replace(code, "&#x", "", -1)
	arr := strings.Split(str, ";")
	if len(arr) == 12 {
		phone = "1"
		n1, _ := strconv.ParseInt(arr[0], 16, 32)
		for k, v := range arr {
			if k != 0 && v != "" {
				n, _ := strconv.ParseInt(v, 16, 32)
				num := n - n1 + 1
				phone += strconv.Itoa(int(num))
			}
		}
	}
	return phone
}
